package com.file.manage.infrastructure.dao.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.file.manage.domain.param.UserQueryParam;
import com.file.manage.infrastructure.dao.UserDao;
import com.file.manage.domain.entity.User;
import com.file.manage.infrastructure.base.dao.impl.BaseDaoImpl;
import com.file.manage.infrastructure.mapper.UserMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author H41
 * @Type UserDao.java
 * @date 2025/5/31 12:01
 * @Desc
 */
@Repository
public class UserDaoImpl extends BaseDaoImpl<UserMapper, User> implements UserDao {
    @Override
    public Map<String, User> mapByIds(List<String> userIds) {
        List<User> users = this.listByIds(userIds);
        return users.stream().collect(Collectors.toMap(User::getId, user -> user));
    }

    @Override
    public List<User> selectUserByParam(UserQueryParam param) {
        if (param == null) return this.list();
        return this.baseMapper.selectList(new LambdaQueryWrapper<User>()
                .eq(StrUtil.isNotEmpty(param.getId()), User::getId, param.getId())
                .like(StrUtil.isNotEmpty(param.getName()), User::getName, param.getName())
                .eq(param.getRole() != null, User::getRole, param.getRole())
                .orderByDesc(User::getCreatedTime)
        );
    }

    @Override
    public List<User> selectUserByName(String userName) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<User>()
                .like(User::getName, userName)
                .orderByDesc(User::getCreatedTime));
    }

    @Override
    public Page<User> pageUserByParam(Page<User> page, UserQueryParam param) {
        if (param == null) return this.page(page);
        return this.baseMapper.selectPage(page, new LambdaQueryWrapper<User>()
                .eq(StrUtil.isNotEmpty(param.getId()), User::getId, param.getId())
                .like(StrUtil.isNotEmpty(param.getName()), User::getName, param.getName())
                .eq(param.getRole() != null, User::getRole, param.getRole())
                .orderByDesc(User::getCreatedTime)
        );
    }
}
